package com.ruoyi.file.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.SysFile;
import com.ruoyi.common.core.domain.WxMaCodeVo;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.file.service.ISysFileService;
import com.ruoyi.file.service.WxFileService;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 文件请求处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/file")
public class SysFileController {
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private ISysFileService sysFileService;
    @Autowired
    private WxFileService wxFileService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file, String fileName) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file, fileName);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return R.ok(sysFile);
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }

    @PostMapping(value="createWxMaCode",produces = "application/octet-stream")
    @ApiOperation(value = "")
    public void createWxMaCode(@RequestBody WxMaCodeVo vo, HttpServletResponse response) throws IOException, WxErrorException {
        wxFileService.createMaUnLimitCode(vo, response);
    }
    @GetMapping("createSDSchoolCodeTest")
    public void createSDSchoolCode(HttpServletResponse response,int weight,int height,int x,int y,String name) throws IOException, WxErrorException, InterruptedException {
        wxFileService.createSDSchoolCode(response,weight,height,x,y,name);
    }
    @GetMapping("createSDSchoolCode")
    public void createSDSchoolCode(HttpServletResponse response) throws IOException, WxErrorException, InterruptedException {
        wxFileService.createSDSchoolCode(response,246,246,18,18,"");
    }

    @PostMapping("/batchUpload")
    public R<List<SysFile>> batchUpload(List<MultipartFile> files, String fileName) {
        List<SysFile> result = new ArrayList<>();
        if (files.size() == 0) return R.fail("请至少选择一个文件");
        for (MultipartFile file : files) {
            if (file.isEmpty()) continue;
            String url = null;
            try {
                url = sysFileService.uploadFile(file, fileName);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            result.add(sysFile);
        }
        return R.ok(result);
    }
}